import lodashObj from 'lodash'

export default class ObjectUtil {
  static getQueryObject(count) {
    const obj = { list: [], page: 0, limit: 10, loading: false, hasMore: true, isEmpty: false, pages: 1, total: 0 }
    if (ObjectUtil.isNull(count) || count === 1) {
      return ObjectUtil.deepCopyObject(obj)
    } else {
      const pArr = []
      for (let i = 0; i < count; i++) {
        pArr.push(ObjectUtil.deepCopyObject(obj))
      }
      return pArr
    }
  }
  static deepCopyObject(obj) {
    try {
      return JSON.parse(JSON.stringify(obj))
    } catch (e) {
      return null
    }
  }

  static objectToArray(obj) {
    if (ObjectUtil.isNull(obj)) {
      return []
    }
    if (ObjectUtil.isString(obj)) {
      return [obj]
    }
    return lodashObj.toArray(obj)
  }
  // 从list 中查找propkey字段值等于targetvalue的对象，然后返回该对象的propvalue
  static findInList = function(list, propkey, targetvalue, propvalue) {
    if (!list) return ''
    for (const key in list) {
      // console.log(propkey)
      // console.log(list, key, list[key][propkey])
      // console.log(targetvalue + ' ? == ' + list[i][propkey] + ': ')
      if (list[key][propkey] === targetvalue) {
        return list[key][propvalue]
      }
    }
    return ''
  }
  static decodeURIComponentForList= function(list, prop_list) {
    if (!list || !prop_list) {
      return
    }
    list.forEach(item => {
      // console.log(item, prop_list)
      prop_list.forEach(key => {
        if (item[key]) {
          item[key] = decodeURIComponent(item[key])
        }
      })
    })
  }

  // 生成随机串
  static randomString= function(len) {
    const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1*** */
    const maxPos = $chars.length
    let pwd = ''
    for (let i = 0; i < len; i += 1) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
    }
    return pwd
  }
  static getRange(start, end, step) {
    return lodashObj.range(start, end, step)
  }
  static isFunction(obj) {
    return lodashObj.isFunction(obj)
  }
  static isDate(obj) {
    return lodashObj.isDate(obj)
  }
  static isString(obj) {
    return lodashObj.isString(obj)
  }
  static isNumber(obj) {
    return lodashObj.isNumber(obj)
  }
  static isArrayLike(obj) {
    return lodashObj.isArrayLikeObject(obj)
  }
  static isArray(obj) {
    return lodashObj.isArray(obj)
  }
  static isEmpty(obj) {
    return lodashObj.isEmpty(obj)
  }
  static notEmpty(obj) {
    // alert(obj)
    return !ObjectUtil.isEmpty(obj)
  }
  static isNull= function(obj) {
    if (lodashObj.isNil(obj) || lodashObj.isUndefined(obj)) {
      return true
    }
    return false
  }
  static notNull= function(obj) {
    return !ObjectUtil.isNull(obj)
  }
  static anyIsNull= function(objList) {
    if (ObjectUtil.isNull(objList)) {
      return true
    }
    for (const obj in objList) {
      if (ObjectUtil.isNull(obj)) {
        return true
      }
    }
    return false
  }
  static allIsNull= function(objList) {
    if (ObjectUtil.isNull(objList)) {
      return true
    }
    for (const key in objList) {
      if (ObjectUtil.notNull(objList[key])) {
        return false
      }
    }
    return true
  }
  static allNotNull= function(objList) {
    return !ObjectUtil.anyIsNull(objList)
  }

  static splitToArray= function(str, splitter) {
    if (!str) {
      return []
    }
    splitter = splitter || ','
    return ObjectUtil.confirmArray(str.split(splitter))
  }

  static confirmValid= function(obj, defaultValue) {
    if (ObjectUtil.notNull(obj)) {
      return obj
    } else {
      return defaultValue
    }
  }

  static confirmValidArray= function(obj) {
    if (lodashObj.isArrayLikeObject(obj)) {
      return obj
    } else {
      return []
    }
  }

  static confirmValidObj= function(obj) {
    return ObjectUtil.confirmValid(obj, {})
  }

  // 如果data不是数组或空，或未定义，则设置为数组
  static confirmArray= function(data) {
    return ObjectUtil.confirmValidArray(data)
  }

  static lengthGreateThenZero = function(obj) {
    if (ObjectUtil.isNull(obj)) {
      return false
    }
    return obj.length > 0
  }
}
